<?php
/**
 * Created by PhpStorm.
 * Login: Administrator
 * Date: 2017-11-14
 * Time: 16:49
 */

namespace app\index\model;

use think\Db;
use think\Model;

class Jurisdiction extends Model
{
    public function jurisdiction()
    {
        //获取添加角色页面的权限数据
        $jurisdictions = [];
        $jurisdiction = Db::table('oa_jurisdiction')->where('pid=0 and state=1')->select();
        foreach ($jurisdiction as $key => $value) {
            $jurisdiction_a = Db::table('oa_jurisdiction')->where('state=1 and pid=' . $value['id'])->select();
            $value['url'] = $jurisdiction_a;
            $jurisdiction = $value;
            $jurisdictions[] = $jurisdiction;
        }
        sort($jurisdictions);
        return $jurisdictions;
    }

    public function role_add($res)
    {
        //添加新角色
        $result_end = 0;
        $num = 0;
        $jurisdiction_id = [];
        if (!empty($res['role'])) {
            //角色名不能重复
            $result = Db::table('oa_role')->where(['role' => $res['role']])->select();
            if (count($result)) {
                return $result_end;
            } else {
                //添加角色
                $arr = Db::table('oa_role')->field('role,descript')->insertGetId($res);
                $jurisdiction = Db::table('oa_jurisdiction')->field('id')->where('state=1')->select();
                foreach ($jurisdiction as $key => $value) {
                    $jurisdiction_id[] = $value['id'];
                }
                // dump($jurisdiction_id);die;
                if (count($jurisdiction_id)) {
                    //选择权限是返回添加权限与角色关联数据成功的影响行数
                    for ($i = 0; $i < count($jurisdiction_id); $i++) {
                        //先存入所有权限
                        $datea['jurisdiction_id'] = '';
                        $datea['role_id'] = $arr;
                        $datea['jurisdiction_id'] = $jurisdiction_id[$i];
                        $result_end = Db::table('oa_role_jurisdiction')->insert($datea);
                    }
                    //unset与角色有关的数据
                    unset($res['role']);
                    unset($res['descript']);
                    unset($res['submit']);
                    //取的选中的权限的id
                    $res = array_values($res);
                    if (count($res)) {
                        for ($j = 0; $j < count($res); $j++) {
                            //在存入所选中的权限
                            $dates['role_id'] = $arr;
                            $dates['jurisdiction_id'] = $res[$j];
                            $date['state'] = 1;
                            Db::table('oa_role_jurisdiction')->where($dates)->update($date);
                            $num++;
                        }
                        return $num;
                    } else {
                        //没有选择权限是返回添加角色成功的id
                        return $arr;
                    }
                }
            }
        } else {
            return $result_end;
        }
    }

    public function role_save($jurisdiction)
    {
        //处理提交修改的权限
        $role['id'] = $jurisdiction['id'];
        $role['role'] = $jurisdiction['role'];
        $role['descript'] = $jurisdiction['descript'];
        $qa = Db::table('oa_role')->where(['role' => $role['role']])->select();
        $qb = Db::table('oa_role')->where(['role' => $role['role'], 'id' => $role['id']])->select();
        if (empty($qa) || !empty($qb)) {
            Db::table('oa_role')->update($role);
            $data['state'] = 0;
            Db::table('oa_role_jurisdiction')->where('role_id=' . $role['id'])->update($data);
//删除表里原始的数据
            $clear = Db::table('oa_role_jurisdiction')->where('role_id=' . $jurisdiction['id'])->delete();
            if ($clear) {
                //初始化表里的数据
                $jurisdiction_a = Db::table('oa_jurisdiction')->where('state=1')->select();
                foreach ($jurisdiction_a as $kk => $vv) {
                    $data['jurisdiction_id'] = $vv['id'];
                    $data['role_id'] = $jurisdiction['id'];
                    Db::table('oa_role_jurisdiction')->insert($data);
                }
            }
            unset($jurisdiction['id']);
            unset($jurisdiction['role']);
            unset($jurisdiction['descript']);
            $jurisdiction_id = array_values($jurisdiction);
            for ($i = 0; $i < count($jurisdiction_id); $i++) {
                $where['role_id'] = $role['id'];
                $where['jurisdiction_id'] = $jurisdiction_id[$i];
                $date['state'] = 1;
                Db::table('oa_role_jurisdiction')->where($where)->update($date);
            }
            return 1;
        } else {
            if (!empty($qa) && empty($qb)) {
                return 2;
            } else {
                return 0;
            }
        }
    }

    public function jurisdiction_role($role_id)
    {
        //添加权限时添加了所有的权限状态为1才是角色所拥有的权限  这样方便取数据
        $result = Db::table('oa_role_jurisdiction r')->field('r.role_id,j.id,j.jurisdiction,r.state')->join('oa_jurisdiction j', 'r.jurisdiction_id = j.id', 'left')->where('j.pid = 0 and r.role_id=' . $role_id)->select();
        foreach ($result as $k => $v) {
            $result_a = Db::table('oa_role_jurisdiction r')->field('r.role_id,j.id,j.url,j.jurisdiction,r.state')->join('oa_jurisdiction j', 'r.jurisdiction_id = j.id', 'left')->where('j.pid=' . $v['id'])->where('r.role_id=' . $role_id)->select();
            $result[$k]['child'] = $result_a;
        }
        sort($result);
        return $result;
    }

    public function role_message($id)
    {
        //获取角色名和，描述
        $result = Db::table('oa_role')->where('id=' . $id)->find();
        return $result;
    }

}